<?php
include_once 'fileupload.class.php';
header("Content-type:text/html;charset=utf-8"); 
	$app = [];
	$app['config'] = require '../../config.php'; 
	require '../../Core/DataBase/ConnetDB.php';
	require '../../Core/DataBase/RequestDB.php';
	require '../../Request/VideoRequest.php';
	
	$pdo = ConnetDB::make($app['config']['database']);
	$quesy = new RequestDB($pdo);
	$quesyvideo = new VideoRequest($pdo);
	

class JsonFormat
{
	public $data;
	public $html;
	public $rows;
	public $OKitems=array();
}

$action=isset($_GET['act'])?$_GET['act']:null;
switch ($action)
{	
    case "upload":
		$file=isset($_FILES["file"])?$_FILES["file"]:null;

		$up=new FileUpload;
		$r=null;
		$isupdated=$up->upload($file);
		$filename=$up->getsourcename(); 
		$filename=str_replace(strrchr($filename, "."),"",$filename); 
		$filenamera=$up->getFileName();
		$filenamera=str_replace(strrchr($filenamera, "."),"",$filenamera); 
		$r.=$isupdated?"[<font style='color:#FF9900'>信息</font>]: [ ".$up->getFileName().">>>===源文件名称===>>>".$filename." ]上传成功! 请等待获取封面再关闭该程序！<br>":$up->getErrorMsg();
		//检测是否有被删除的空闲用户如果有则替换掉
		$statement = $pdo ->prepare("select * from videos where isdel=1");
		$statement -> execute();
		$arraydelids = $statement -> fetchAll();
		if($arraydelids!=null){
			$arryids = $arraydelids[0][0];
		}else{
			$arryids = null;
		}
		$statement = $pdo ->prepare("UPDATE `videos` SET `name`='$filename', `vid`='$filenamera' , `isdel`='0' WHERE  `id`='$arryids'");
		$statement -> execute();
		}else{
		$statement = $pdo ->prepare("insert videos(name,vid) values('".$filename."','".$filenamera."')");
		$statement -> execute();
		}
		
		$filevideo=$_SERVER['DOCUMENT_ROOT']."/video/";
		$filevideoimg=$_SERVER['DOCUMENT_ROOT']."/videoimg/";
		exec('ffmpeg -i '.$filevideo.$up->getFileName().' -y -f mjpeg -ss 30 -t 1 -s 241x134 '.$filevideoimg.$filenamera.'.png');
		
		$r.=$isupdated?"[<font style='color:#FF9900'>信息</font>]: [".$up->getFileName()."]获取封面成功!<br>":$up->getErrorMsg();
		
		$res=new JsonFormat;
		$res->html=$r;
		$res->data=$isupdated;
		
		ob_clean();
		header('Content-type:application/json');
		print(json_encode($res, JSON_FORCE_OBJECT));
	break;
}
